package com.bocai.controller;

import com.bocai.bean.TSalesUser;
import com.bocai.bean.TUserCode;
import com.bocai.service.ISalesService;
import com.bocai.service.IUserService;
import com.bocai.session.BaseSession;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.MD5Utils;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController extends BaseSession {

    @Autowired
    private ISalesService salesService;

    @Autowired
    private IUserService userService;

    @ApiOperation("二级分销商登录")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", dataType = "string", required = true, value = "手机号", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "password", dataType = "string", required = true, value = "密码", defaultValue = ""),
    })
    @ResponseBody
    public ResponseData login(String phone, String password) {
        ResponseData data = new ResponseData();
        data = salesService.loginSaleMethod(phone, password, data);
        if (data.getCode().equals(CodeEnum.SUCCESS.getCode())) {
            TSalesUser salesUser = (TSalesUser) data.getResults();
            getRequest().getSession().setAttribute("salesId", salesUser.getSalesId());
            getRequest().getSession().setAttribute("salesPhone", salesUser.getSalesPhone());
        }
        return data;
    }

    @ApiOperation("二级分销商修改密码")
    @RequestMapping(value = "/modifyPwd", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", dataType = "string", required = true, value = "手机号", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "oldPwd", dataType = "string", required = true, value = "旧密码", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "newPwd", dataType = "string", required = true, value = "新密码", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "code", dataType = "string", required = true, value = "验证码", defaultValue = ""),
    })
    @ResponseBody
    public ResponseData modifyPwd(String phone, String oldPwd, String newPwd, String code) {
        ResponseData data = new ResponseData();
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{phone, oldPwd, newPwd, code});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        TSalesUser salesUser = this.salesService.selectSaleForPhone(phone);
        if (null == salesUser) {
            data.setCodeEnum(CodeEnum.CODE_10003);
            return data;
        }

        TUserCode userCode = this.userService.selectUserCode(phone);

        if (null == userCode) {
            data.setCodeEnum(CodeEnum.CODE_10001);
            return data;
        }

        if (!code.equals(userCode.getUserCode())) {
            data.setCodeEnum(CodeEnum.CODE_10001);
            return data;
        }

        if (!salesUser.getSalesPassword().equals(MD5Utils.md5Password(oldPwd))) {
            data.setCodeEnum(CodeEnum.CODE_10002);
            return data;
        }

        salesUser.setSalesPassword(MD5Utils.md5Password(newPwd));
        if (this.salesService.updateSale(salesUser) > 0) {
            data.setCodeEnum(CodeEnum.SUCCESS);
            return data;
        }
        data.setCodeEnum(CodeEnum.CODE_1);
        return data;
    }

    @ApiOperation("二级分销商获取验证码")
    @RequestMapping(value = "/sendSMS", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", dataType = "string", required = true, value = "手机号", defaultValue = ""),
    })
    @ResponseBody
    public ResponseData sendSMS(String phone) {
        ResponseData data = new ResponseData();

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{phone});
        if (ObjUtils.isNotEmpty(codeEnum)) {
            data.setCodeEnum(CodeEnum.CODE_90002);
            return data;
        }

        TUserCode userCode = new TUserCode();
        userCode.setUserCode("888888");
        userCode.setUserPhone(phone);

        if (this.userService.insertCode(userCode) != null) {
            data.setCodeEnum(CodeEnum.SUCCESS);
            data.setResults(userCode);
        } else {
            data.setCodeEnum(CodeEnum.CODE_1);
        }


        return data;
    }
}
